import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import "rabbitmqmodel"
import "models"

Item {
    id: root
    QtObject {
        id: id_sub_state
        property bool sub_precedure_state: false
        property bool sub_storage_state: false
        property int sub_storage_id: 0
        property int sub_precedure_id: 0
    }
    Text {
        id: id_page1_title
        text: qsTr("页面3")
        anchors.horizontalCenter: root.horizontalCenter
        anchors.top: root.top
    }
    RowLayout {
        id: id_page1_layout
        anchors.top: id_page1_title.bottom
        width: root.width
        Item {
            Layout.fillWidth: true
        }
        Button {
            id: id_button_sub_procedure
            width: 100
            height: 100
            text: "订阅procedure"
            onClicked: {
                console.log("click id_button_sub_procedure")
                if (id_sub_state.sub_precedure_state === true) {
                    RabbitmqManager.unsubscribe(id_sub_state.sub_precedure_id)
                    id_button_sub_procedure.text = "订阅procedure"
                    id_sub_state.sub_precedure_state = false
                } else {
                    id_button_sub_procedure.text = "解除procedure订阅"
                    id_sub_state.sub_precedure_state = true
                    id_sub_state.sub_precedure_id = RabbitmqManager.subscribe(
                                Utils._SERVICE_NAME_PROCEDURE, function (msg) {
                                    console.log("recv PrecedureService:", msg)
                                    id_edit.append(msg)
                                })
                }
            }
        }

        Button {
            id: id_button_sub_storage
            width: 100
            height: 100
            text: "订阅storage"
            onClicked: {
                console.log("click id_button_sub_storage")
                if (id_sub_state.sub_storage_state === true) {
                    RabbitmqManager.unsubscribe(id_sub_state.sub_storage_id)
                    id_button_sub_storage.text = "订阅Storage"
                    id_sub_state.sub_storage_state = false
                } else {
                    id_button_sub_storage.text = "解除Storage订阅"
                    id_sub_state.sub_storage_state = true
                    id_sub_state.sub_storage_id = RabbitmqManager.subscribe(
                                Utils._SERVICE_NAME_STORAGE, function (msg) {
                                    console.log("recv StorageService:", msg)
                                    id_edit.append(msg)
                                })
                }
            }
        }
        Button {
            id: id_button_clear
            width: 100
            height: 100
            text: "清除消息"
            onClicked: {
                id_edit.clear()
            }
        }
        Item {
            Layout.fillWidth: true
        }
    }
    Flickable {
        id: id_flick
        anchors.top: id_page1_layout.bottom
        width: root.width
        height: 350
        contentWidth: id_edit.paintedWidth
        contentHeight: id_edit.paintedHeight
        clip: true
        function ensureVisible(r) {
            if (contentX >= r.x)
                contentX = r.x
            else if (contentX + width <= r.x + r.width)
                contentX = r.x + r.width - width
            if (contentY >= r.y)
                contentY = r.y
            else if (contentY + height <= r.y + r.height)
                contentY = r.y + r.height - height
        }

        TextEdit {
            id: id_edit
            width: id_flick.width
            focus: true
            wrapMode: TextEdit.Wrap
            onCursorRectangleChanged: id_flick.ensureVisible(cursorRectangle)
        }
    }
}
